/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WaitingRoom;

import ServerClient.Client;
import ServerClient.Client;
import factory.Constants;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author PhucAnh
 */
public class GameInClient extends javax.swing.JFrame implements ActionListener {

    /**
     * Creates new form mainGameInterface
     */
    int[][] check;
    JButton[][] arrayOfButton;
    Client clientSend;
    boolean flag = true;
    ImageIcon icon1 = new ImageIcon(Constants.IMAGE_PLAYER_1);
    ImageIcon icon2 = new ImageIcon(Constants.IMAGE_PLAYER_2);
    Thread th;
//    public static void main(String[] args) {
//        new GameInClient(new User());
//    }

    public GameInClient() {
        initComponents();
        receiveThread();
        sendThread();
        setVisible(true);
        setSize(800,800);
        setResizable(false);
    }

    public static void main(String[] args) {
        new GameInClient();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The conclitent of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
       

        jButton1 = new javax.swing.JButton();
        titlePanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        gamePanel = new javax.swing.JPanel();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setText("player 1: ");

        jLabel2.setText("player 2:");

        jLabel3.setText("Score 1");

        jLabel4.setText("Score 2");

        jLabel5.setText("Turn 1");

        jLabel6.setText("Turn 2");

        jLabel7.setText("MOVE : ");

        jLabel8.setText("Total Score");

//        jLabel9.setText("Turn");

        titlePanel = new JPanel(new GridLayout(3, 3));
        titlePanel.add(new JLabel());
        titlePanel.add(new JLabel("Score"));
        titlePanel.add(new JLabel("Turn"));
        titlePanel.add(jLabel1);
        titlePanel.add(jLabel3);
        titlePanel.add(jLabel5);
        titlePanel.add(jLabel2);
        titlePanel.add(jLabel4);
        titlePanel.add(jLabel6);
        
        check = new int[20][20];
        arrayOfButton = new JButton[20][20];
        gamePanel = new JPanel();
        gamePanel.setLayout(new GridLayout(20, 20));
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                check[i][j] = -1;
                arrayOfButton[i][j] = new JButton();
                arrayOfButton[i][j].setBackground(Color.WHITE);
                gamePanel.add(arrayOfButton[i][j]);
                arrayOfButton[i][j].addActionListener(this);
            }
        }
        setLayout(new BorderLayout());
        add(titlePanel,BorderLayout.NORTH);
        add(gamePanel,BorderLayout.CENTER);
        add(new JLabel("MOVE"),BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel gamePanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel titlePanel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                if (e.getSource() == arrayOfButton[i][j] && flag == true) {
                    if (check[i][j] == -1) {
                        arrayOfButton[i][j].setIcon(icon2);
                        flag = false;
                            clientSend.sentData(String.valueOf(i));
                            clientSend.sentData(String.valueOf(j));
                        
                    }
                }
            }
        }
    }
    public void sendThread(){
        clientSend = new Client("localhost",Constants.PORT_SEND);
        Thread th2 = new Thread() {
            public void run() {
                try {
                    clientSend.initConnection();
                } catch (Exception ex) {
                    System.out.println(ex.toString());
                }
            }
        };
        th2.start();
    }
    public void receiveThread() {
        th = new Thread() {
            public void run() {
                Client clientRe = new Client("localhost",Constants.PORT_RECEIVE);

                try {
                    clientRe.initConnection();
                    while (true) {
                        String datax = clientRe.receiveData();
                        String datay = clientRe.receiveData();
                        int x = Integer.parseInt(datax);
                        int y = Integer.parseInt(datay);
                        flag = true;
                        arrayOfButton[x][y].setIcon(icon1);
                        check[x][y] = 2;
                        
                    }
                } catch (Exception ex) {
                }
            }
        };
        th.start();
    }
}
